from dateutil.relativedelta import relativedelta

from commons.entity.vo.approval_status_vo import ApprovalStatusVO
from commons.entity.vo.map_data_vo import MapDataVO
from commons.entity.vo.reimbursement_report_vo import ReimbursementReportVO
from report_service.report.mappers.report_mapper import ReportMapper
from datetime import datetime
from typing import List


class ReportService:
    def __init__(self):
        self.report_mapper = ReportMapper()  # Python无类型，一个即可，看完删除这个注释

    def get_bar_data(self, start: datetime, end: datetime) -> List[ReimbursementReportVO]:
        dataList = []
        self.init_data_list(start, end, dataList)

        source_list = self.report_mapper.select_sum_count_by_date(start, end)
        for r in source_list:
            year = r.get_apply_date().year
            month = r.get_apply_date().month - 1  # Python中的月份从0开始

            for report in dataList:
                report_year = report.date.year
                report_month = report.date.month - 1  # 调整为0-based索引

                if report_year == year and report_month == month:
                    total_cost = r.get_food_cost() + r.get_transport_cost() + r.get_hotel_cost()
                    report.count += total_cost
                    break
        return dataList

    def init_data_list(self, start: datetime, end: datetime, data_list: List[ReimbursementReportVO]):
        current_date = start
        while current_date <= end:
            data_list.append(ReimbursementReportVO(current_date))  # 假设ReimbursementReportVO接受一个datetime对象作为参数
            current_date += relativedelta(months=1)  # 增加一个月

    def get_pie_data(self, start_date: datetime, end_date: datetime) -> ApprovalStatusVO:
        approval_status_vo = ApprovalStatusVO()
        approval_list = self.report_mapper.select_approval_by_date(start_date, end_date)

        for approval in approval_list:
            if approval.get_travel_approval() == 1:
                approval_status_vo.travel_approval_pass_num += 1
            else:
                approval_status_vo.travel_approval_no_pass_num += 1

            if approval.get_advance_approval() == 1:
                approval_status_vo.advance_approval_pass_num += 1
            else:
                approval_status_vo.advance_approval_no_pass_num += 1

            if approval.get_reimbursement_approval_ma() == 1:
                approval_status_vo.reimbursement_approval_ma_pass_num += 1
            else:
                approval_status_vo.reimbursement_approval_ma_no_pass_num += 1

            if approval.get_reimbursement_approval_fi() == 1:
                approval_status_vo.reimbursement_approval_fi_pass_num += 1
            else:
                approval_status_vo.reimbursement_approval_fi_no_pass_num += 1

        return approval_status_vo

    def get_map_data(self, start_date: datetime, end_date: datetime) -> List[MapDataVO]:
        map_data_vo_list = self.init_map_data_list()
        source_list = self.report_mapper.select_schedule_by_date(start_date, end_date)

        for schedule in source_list:
            location = schedule.get_location()
            if location.startswith("广东"):
                map_data_vo_list[0].increment_value()
            elif location.startswith("江苏"):
                map_data_vo_list[1].increment_value()
            elif location.startswith("山东"):
                map_data_vo_list[2].increment_value()
            elif location.startswith("浙江"):
                map_data_vo_list[3].increment_value()
            elif location.startswith("四川"):
                map_data_vo_list[4].increment_value()
            elif location.startswith("河南"):
                map_data_vo_list[5].increment_value()
            elif location.startswith("湖北"):
                map_data_vo_list[6].increment_value()
            elif location.startswith("福建"):
                map_data_vo_list[7].increment_value()
            elif location.startswith("台湾"):
                map_data_vo_list[8].increment_value()
            elif location.startswith("湖南"):
                map_data_vo_list[9].increment_value()
            elif location.startswith("上海"):
                map_data_vo_list[10].increment_value()
            elif location.startswith("安徽"):
                map_data_vo_list[11].increment_value()
            elif location.startswith("河北"):
                map_data_vo_list[12].increment_value()
            elif location.startswith("北京"):
                map_data_vo_list[13].increment_value()
            elif location.startswith("陕西"):
                map_data_vo_list[14].increment_value()
            elif location.startswith("江西"):
                map_data_vo_list[15].increment_value()
            elif location.startswith("辽宁"):
                map_data_vo_list[16].increment_value()
            elif location.startswith("重庆"):
                map_data_vo_list[17].increment_value()
            elif location.startswith("云南"):
                map_data_vo_list[18].increment_value()
            elif location.startswith("广西"):
                map_data_vo_list[19].increment_value()
            elif location.startswith("香港"):
                map_data_vo_list[20].increment_value()
            elif location.startswith("山西"):
                map_data_vo_list[21].increment_value()
            elif location.startswith("内蒙古"):
                map_data_vo_list[22].increment_value()
            elif location.startswith("贵州"):
                map_data_vo_list[23].increment_value()
            elif location.startswith("新疆"):
                map_data_vo_list[24].increment_value()
            elif location.startswith("天津"):
                map_data_vo_list[25].increment_value()
            elif location.startswith("黑龙江"):
                map_data_vo_list[26].increment_value()
            elif location.startswith("吉林"):
                map_data_vo_list[27].increment_value()
            elif location.startswith("甘肃"):
                map_data_vo_list[28].increment_value()
            elif location.startswith("海南"):
                map_data_vo_list[29].increment_value()
            elif location.startswith("宁夏"):
                map_data_vo_list[30].increment_value()
            elif location.startswith("青海"):
                map_data_vo_list[31].increment_value()
            elif location.startswith("澳门"):
                map_data_vo_list[32].increment_value()
            elif location.startswith("西藏"):
                map_data_vo_list[33].increment_value()
            elif location.startswith("南海诸岛"):
                map_data_vo_list[34].increment_value()

        # Python中对列表进行排序的逻辑
        map_data_vo_list.sort(key=lambda x: x.value, reverse=True)

        return map_data_vo_list

    def init_map_data_list(self) -> List[MapDataVO]:
        return [
            MapDataVO("广东省", 0),
            MapDataVO("江苏省", 0),
            MapDataVO("山东省", 0),
            MapDataVO("浙江省", 0),
            MapDataVO("四川省", 0),
            MapDataVO("河南省", 0),
            MapDataVO("湖北省", 0),
            MapDataVO("福建省", 0),
            MapDataVO("台湾省", 0),
            MapDataVO("湖南省", 0),
            MapDataVO("上海市", 0),
            MapDataVO("安徽省", 0),
            MapDataVO("河北省", 0),
            MapDataVO("北京市", 0),
            MapDataVO("陕西省", 0),
            MapDataVO("江西省", 0),
            MapDataVO("辽宁省", 0),
            MapDataVO("重庆市", 0),
            MapDataVO("云南省", 0),
            MapDataVO("广西壮族自治区", 0),
            MapDataVO("香港特别行政区", 0),
            MapDataVO("山西省", 0),
            MapDataVO("内蒙古自治区", 0),
            MapDataVO("贵州省", 0),
            MapDataVO("新疆维吾尔自治区", 0),
            MapDataVO("天津市", 0),
            MapDataVO("黑龙江省", 0),
            MapDataVO("吉林省", 0),
            MapDataVO("甘肃省", 0),
            MapDataVO("海南省", 0),
            MapDataVO("宁夏回族自治区", 0),
            MapDataVO("青海省", 0),
            MapDataVO("澳门特别行政区", 0),
            MapDataVO("西藏自治区", 0),
            MapDataVO("南海诸岛", 0)
        ]

